package com.formiaomiao.demain.service.impl;

import com.formiaomiao.demain.dao.FriendDao;
import com.formiaomiao.demain.dao.FriendRemarksDao;
import com.formiaomiao.demain.dao.UserDao;
import com.formiaomiao.demain.model.FriendBean;
import com.formiaomiao.demain.model.FriendRemarksBean;
import com.formiaomiao.demain.model.Result;
import com.formiaomiao.demain.model.UserBean;
import com.formiaomiao.demain.service.FriendService;
import com.formiaomiao.demain.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service(value = "friendService")
public class FriendServiceImpl implements FriendService {

    @Autowired
    private FriendDao friendDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private FriendRemarksDao friendRemarksDao;

    @Transactional(value = "demain")
    @Override
    public Result<String> InsertFriend(FriendBean friendBean) {
        UserBean bean = new UserBean();
        bean.setNeteaseUid(friendBean.getNeteaseUid());
        friendBean.setFriendto(userDao.selectUserid(bean).getId());
        if (friendDao.InsertFriend(friendBean) == 0) {
            return ResultUtil.error(400, "添加好友失败");
        } else {
            return ResultUtil.success("添加好友成功");
        }
    }

    @Transactional(value = "demain")
    @Override
    public Result<String> DeleteFriend(FriendBean friendBean) {
        if (friendDao.DeleteFriend(friendBean) == 0 || friendDao.DeletesFriend(friendBean) == 0) {
            return ResultUtil.error(400, "删除好友失败");
        } else {
            FriendRemarksBean friendRemarksBean = new FriendRemarksBean();
            friendRemarksBean.setFriendfrom(friendBean.getFriendfrom());
            friendRemarksBean.setFriendto(friendBean.getFriendto());
            if (friendRemarksDao.DeleteRemarks(friendRemarksBean) == 0) {
                return ResultUtil.error(400, "删除好友失败");
            } else {
                return ResultUtil.success("删除好友成功");
            }
        }
    }

    @Override
    public Result<String> SelectFriend(FriendBean friendBean) {
        if (friendDao.SelectFriend(friendBean) != null && friendDao.SelectFriend(friendBean).size() > 0 ||
                friendDao.SelectsFriend(friendBean) != null && friendDao.SelectsFriend(friendBean).size() > 0) {
            return ResultUtil.success("true");
        } else {
            return ResultUtil.success("false");
        }
    }
}
